package com.iretailer.report.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.iretailer.report.dto.PageResult;
import com.iretailer.report.dto.QueryOption;
import com.iretailer.report.mapper.BusinessTimeMallMapper;
import com.iretailer.report.model.BusinessTimeMall;
import com.iretailer.report.model.MallBusinessTime;
import com.iretailer.report.model.UserMall;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * @Auther: xjw
 * @Date: 2019/3/6 17:19
 * @Description:
 */
@Service
public class BusinessTimeMallService extends ServiceImpl<BusinessTimeMallMapper, BusinessTimeMall> {
    @Autowired
    private BusinessTimeMallMapper businessTimeMallMapper;
    @Autowired
    private UserMallService userMallService;

    public List<MallBusinessTime> selectMallBusinessTime(QueryWrapper queryWrapper) {
        return businessTimeMallMapper.selectMallBusinessTime(queryWrapper);
    }

    public PageResult<MallBusinessTime> gridMallBusinessTime(String userId, String companyName, String mallName, QueryOption queryOption) {
        Page<MallBusinessTime> page = new Page<>(1, Long.MAX_VALUE);
        QueryWrapper<MallBusinessTime> queryWrapper = new QueryWrapper<>();
        //这里设置一个 1 =1 ，当下面条件都没有的时候 不会生成一个 where
        queryWrapper.eq("1", 1);
//        if (companyId != null) {
//            queryWrapper.eq("companyId", companyId);
//        }
        if (companyName != null) {
            queryWrapper.like("companyName", companyName);
        }
        if (mallName != null) {
            queryWrapper.like("mallName", mallName);
        }
        queryOption.judge(page, queryWrapper);
        queryWrapper.orderByAsc("companyName").orderByAsc("MallName");
        if (userId != null) {
//            List<String> childens = userService.getTreeId(userId);
//            childens.add(userId);
//            List<String> mallIds = userMallService.selectMallIdsByUserIds(childens);
//            if (mallIds.size() == 0) {
//                mallIds.add("1");
//            }
            List<String> mallIds = userMallService.selectUserMallIds(userId);
            queryWrapper.in("mallId", mallIds);
        }
        businessTimeMallMapper.selectPageMallBusinessTime(page, queryWrapper);
        return new PageResult<>(page.getTotal(), page.getRecords());
    }

}
